import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $:any;
@Component({
	selector: 'sa-firmware-upload',
	templateUrl: './firmware-upload.component.html',
})
export class FirmwareUploadComponent implements OnInit {
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			
		}
	}
	page: any = {
		version: '',
		description: '',
		packetSize: '1024',
		protocolVersion: '2',
		companyId:'',
		companyName:'',
		useTypeL1:'',
		useTypeL2:'',
		hwVersion:'',
		appSupport:'0',
		isDefault: 0 // 是否是默认固件（0不是 1是）
	};


    fileName: string;
    fileToUpload: File = null;
	page_title:string = '新增';
	pageType:string = 'new';
	carTypeList = carTypeListObj.carTypeList
	constructor(private router: Router, private jsonApiService: JsonApiService) {}
	getFile(files: FileList) {
		this.fileToUpload = files.item(0);
		if(!this.fileToUpload){
			this.jsonApiService.message('请选择文件')
			return false;
		}
        this.fileName = files[0].name;
   	}
	//上传提交
	uploadFile(){
		if(!this.fileToUpload){
     		this.jsonApiService.message('请选择需要上传的文件')
        	return;
        }
		this.page.useTypeL2 = $('#useTypeSelect').val();
		this.page.useTypeL1 = $('#useTypeSelect').val().slice(0,2);
		// 获取是否默认固件
		this.page.isDefault = $('#isDefault').is(':checked') ? 1 : 0
		
		let postParam = new FormData();
		for(let item in this.page){
			postParam.append(item, this.page[item]);
		}
		postParam.append('binFile', this.fileToUpload);
		
        this.jsonApiService.form_http("bininfo/upload.html", postParam).subscribe(data => {
			this.jsonApiService.message(data.message);
			this.goBack();
		})
	}
	onSubmit(e) {}
	//返回列表页
	goBack() {
		this.router.navigate(['/system-config/firmware-management']);
	}
	ngOnInit() {}
	
	//公司选中回调
	companySelected(data){
		this.page.companyId = data.companyId;
		this.page.companyName = data.companyName;
	}
}